﻿Shader "Custom/Unlit/Transparent"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "black" {}
		_Mask("Mask",2D) = "white"{}
		_Color("Color",Color) = (1,1,1,1)
		_ShadowStrngth("ShadowStrngth",Range(0,1)) = 1
	}
	SubShader
	{
		Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
		LOD 100
		Blend SrcAlpha OneMinusSrcAlpha

		CGINCLUDE

		#include "UnityCG.cginc"

		struct appdata
		{
			float4 vertex : POSITION;
			float2 uv : TEXCOORD0;
		};

		struct v2f
		{
			float2 uv : TEXCOORD0;
			float4 vertex : SV_POSITION;
		};

		sampler2D _MainTex;
		sampler2D _Mask;
		float4 _MainTex_ST;
		fixed4 _Color;
		uniform float flipflag;
		uniform float cutoutflag;
		uniform float4 mpos;
		float _ShadowStrngth;

		v2f vertback(appdata v)
		{
			v2f o;

			if (flipflag > 0)
			{
				v.uv.y = 1 - v.uv.y;
			}

			//mpos：鼠标位置相对于图片的位置，以图片中心为零点
			float2 vec = v.vertex.xy - mpos.xy;
			//对显示图片的网格顶点进行缩放
			v.vertex.xy = mpos.xy + vec * 1.05;
			o.vertex = UnityObjectToClipPos(v.vertex);
			o.uv = v.uv;
			return o;
		}

		v2f vert(appdata v)
		{
			v2f o;

			if (flipflag > 0)
			{
				v.uv.y = 1 - v.uv.y;
			}

			o.vertex = UnityObjectToClipPos(v.vertex);
			o.uv = v.uv;
			return o;
		}

		fixed4 fragBack(v2f i):SV_Target
		{
			fixed4 col = tex2D(_MainTex, i.uv);
			fixed mask = tex2D(_Mask, i.uv).r;
			col.a *= mask;
			//alpha为0的地方全黑
			col.rgb = lerp(fixed3(0,0,0),fixed3(0.05, 0.05, 0.05),col.a);
			col.a *= 0.6;
			return col * _Color * _ShadowStrngth;
		}

		fixed4 frag(v2f i) : SV_Target
		{
			fixed4 col = tex2D(_MainTex, i.uv);
			fixed mask = tex2D(_Mask, i.uv).r;
			col.a *= mask;
			return col * _Color;
		}
		ENDCG

		Pass
		{
			offset 1,1
			CGPROGRAM
			#pragma vertex vertback
			#pragma fragment fragBack

			ENDCG

		}

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			ENDCG
			
		}
	}
}
